---@class UITimeBoxBag : UIBase
UITimeBoxBag = UIBase.New(PanelResNames.UITimeBoxBag)
local t = UITimeBoxBag
local _sequence = 0
function t.Init(sequence)
    _sequence = sequence
    UIBase.Init(t)
end

function t:OnCreate()
    self.configData = G.dataTable["t_stage"][tostring(logicMgr.MapManager.getMapId())]
    self.idList = TimeBoxHandler.CONST_BOX_ID_LIST
    self.boxItemList = {}
    for i = 0 ,3 do
        local item = {}
        local box = self:GetChild("box"..i)
        local box_box = self:GetChild("box",box)
        item.icon = self:GetChild("icon",box_box)
        item.num = self:GetChild("num",box_box)
        item.quality = self:GetChild("quality",box_box)
        item.frame = self:GetChild("frame",box_box)
        item.title = self:GetChild("box"..i.."/title")
        item.time_box_text = self:GetChild("time-box",box)
        local ctrlTime = self:GetController("time-box",box)
        ctrlTime.selectedIndex = 1
        self.boxItemList[i+1] = item
    end
    self.rewardList = self:GetChild("rewardList")
    self.tips = self:GetChild("tips")
    self.ctrlSelect = self:GetController("ctrlSelect")
    self.cancelBtn = self:GetChild("cancelBtn")
    self.sureBtn = self:GetChild("sureBtn")
    self.backBtn = self:GetChild("backBtn")
    self:RegisterOnClick(self.cancelBtn,self.ClickCancel )
    self:RegisterOnClick(self.sureBtn,self.ClickSure)
    self:RegisterOnClick(self.backBtn,self.ClickCancel)
    self:RegisterOnChanged(self.ctrlSelect,self.SetData)
    self:SetDynamicList(self.rewardList,self.ItemRender)
    self.tips.text = string.format(getLanguage("TimeBox_Tips1"),1)
end

function t:SetData()
    self.wards = {}
    self.wards = logicMgr.ItemManager.ParseRewards(self.configData["f_MagicBox"..self.ctrlSelect.selectedIndex+1])
    self.rewardList.numItems = #self.wards
end

function t:ItemRender(idx,obj)
    local data = self.wards[idx]
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
    itemCtr.initData(obj, data);
end

function t:Refresh()
    local flag = true
    for i = #self.idList,1,-1 do
        local id = self.idList[i]
        local item = self.boxItemList[i]
        local num =  logicMgr.ItemManager.getBagNumByTableID(id)
        item.num.text = num
        item.title.text = logicMgr.ItemManager.GetItemName(id)
        self:SetItemAllById(item.icon,item.quality,item.frame, id)
        if num > 0 and flag then
            self.ctrlSelect.selectedIndex = i-1
            flag = false
        end
        local cfg = dataTable.getDataTableByStringId("t_item",id)
        local itemFunction = decodeJsonStrToTabel(cfg.f_ItemFunction)
        item.time_box_text.text = string.format(getLanguage("Sys_Time_Hour"),itemFunction[2]/3600)
    end
end
function t:OnEnable()
    self:SetData()
    self:Refresh()
end
function t:OnDisable()
end
function t:OnDestroy()
end
function t:ClickCancel()
    closeUI(PanelResNames.UITimeBoxBag)
end
function t:ClickSure(params)
    local index = self.ctrlSelect.selectedIndex
    local modelId = self.idList[index+1]
    if not modelId then return end
    local count = logicMgr.ItemManager.getBagNumByTableID(modelId)
    if count == 0 then
        UISysTips.AddMsg(getLanguage("TimeBoxNoBoxTip"))
        return
    end
    local id = logicMgr.ItemManager.getItemIdByModelId(modelId)

    local tb = TimeBoxHandler.GetBoxRemainingState()
    local items = {0,0,0,0,0,0}
    local start = (_sequence-1) * TimeBoxHandler.MAX_COUNT + 1
    for i = start,#tb do
        if tb[i] == 1 then
            items[i] = id
            break
        end
    end
    TimeBoxHandler.ReqTimeBoxAddBox(1,0, items )
    self:ClickCancel()
end
return t